local dingpin = fk.CreateSkill{
    name = "wk_heg__dingpin",
}

local H = require "packages.ol_hegemony.util"
local HG = require "packages.new_heg_diy_my.util"

Fk:loadTranslationTable{
    ["wk_heg__dingpin"] = "定品",
    [":wk_heg__dingpin"] = "结束阶段，你可叠置并令一名其他角色于此回合结束后执行一个额外回合，此额外回合：1.准备阶段，若其与你势力相同，"..
    "其<a href=':ElectedChange'>推举</a>；2.回合结束时，其叠置。",

    ["@@wk_heg__dingpin_extra"] = "定品",
    ["#wk_heg__dingpin-choose"] = "定品：你可以选择一名其他角色，令其于此回合结束后执行一个额外回合",
    ["#wk_heg__dingpin_delay"] = "定品",

    ["$wk_heg__dingpin1"] = "取才赋职，论能行赏。",
    ["$wk_heg__dingpin2"] = "定品寻良骥，中正探人杰。",
}

dingpin:addEffect(fk.EventPhaseStart,{
  anim_type = "support",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(dingpin.name) and player == target and player.phase == Player.Finish
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room.alive_players, function(p) return p ~= player end)
    local to = room:askToChoosePlayers(player,{
        targets = targets,
        min_num = 1,
        max_num = 1,
        prompt = "#wk_heg__dingpin-choose",
        skill_name = dingpin.name,
        cancelable = true,
    })
    if #to > 0 then
      event:setCostData(self,{to = to})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).to[1]
    if H.compareKingdomWith(player, to) then
      room:setPlayerMark(to, "_wk_heg__dingpin", 1)
    else
      room:setPlayerMark(to, "_wk_heg__dingpin", 100)
    end
    player:turnOver()
    to:gainAnExtraTurn(true, dingpin.name)
  end,
})

dingpin:addEffect(fk.EventPhaseStart,{
  is_delay_effect = true,
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return target.phase == Player.Start and target:getCurrentExtraTurnReason() == "wk_heg__dingpin"
      and player:getMark("_wk_heg__dingpin") > 0 and player:getMark("_wk_heg__dingpin") < 100 and player == target
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:notifySkillInvoked(player, dingpin.name, "support")
    player:broadcastSkillInvoke(dingpin.name)
    HG.DoElectedChange(room, target, dingpin.name)
  end,
})

dingpin:addEffect(fk.TurnEnd,{
  is_delay_effect = true,
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    return player:getMark("_wk_heg__dingpin") > 0 and target:getCurrentExtraTurnReason() == "wk_heg__dingpin" and player == target
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(target, "_wk_heg__dingpin", 0)
    target:turnOver()
  end,
})

return dingpin